// pages/address/address_add/address_add.js
const api = require('../../../utils/util.js');
const service = require('../../../utils/service.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    cityData: [],
    city2Data: [],
    city3Data: [],
    city: [
      [],
      [],
      []
    ],
    multiIndex: [0, 0, 0],
    name: '',
    phone: '',
    province_id: '',
    city_id: '',
    district_id: '',
    address: '',
    postcode: '',
    status: 1, //-- 1默认地址 0不是默认地址
    addressArr: [],
    region: ''
  },

  formSubmit: function(e) {
    console.log(e.detail.value);
    let data = e.detail.value;
    if (data.name == '') {
      api.toast('请填写收货人');
      return;
    } else if (data.phone == '') {
      api.toast('请填写联系电话');
      return;
    } else if (!api.checkPhone(data.phone)){
      api.toast('请输入正确的电话号码');
      return;
    } else if (data.address == '') {
      api.toast('请输入详细地址');
      return;
    }
    var province_id = this.data.cityData[this.data.multiIndex[0]].zone_id;
    var city_id = this.data.city2Data[this.data.multiIndex[1]].zone_id;
    var district_id = this.data.city3Data[this.data.multiIndex[2]].zone_id;

    api.ajax(service.url('addMyAddress'), {
      address_id: data.address_id,
      name: data.name,
      phone: data.phone,
      province_id: province_id,
      city_id: city_id,
      district_id: district_id,
      address: data.address,
      postcode: '',
      status: this.data.status,
    }, function(res) {
      console.log(res);
      wx.showModal({
        title: '信息提示',
        cancelText: '继续添加',
        content: '添加地址成功！',
        success: function(res) {  
          if (res.confirm) {   
            wx.navigateBack({
              success: function() {
                if (getCurrentPages().length != 0) {
                  //刷新当前页面的数据
                  getCurrentPages()[getCurrentPages().length - 1].onLoad()
                }
              }
            })  
          } 
        }
      })

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let _this = this;

    //--第一列初始数据
    api.ajax(service.url('getZoneList'), {
      
      pid: 0
    }, function(res) {
      console.log(res)
      //--把第一列原始数据存起来
      _this.setData({
        cityData: res.data
      })

      let city = [
        [],
        [],
        []
      ];
      for (var i = 0; i < res.data.length; i++) {
        city[0].push(res.data[i].name)
      }

      //--第二列初始数据
      api.ajax(service.url('getZoneList'), {
        
        pid: res.data[0].zone_id
      }, function(res) {
        //--把第二列原始数据存起来
        _this.setData({
          city2Data: res.data
        })
        for (var i = 0; i < res.data.length; i++) {
          city[1].push(res.data[i].name)
        }

        //--第三列初始数据
        api.ajax(service.url('getZoneList'), {
          
          pid: res.data[0].zone_id
        }, function(res) {
          //--把第三列原始数据存起来
          _this.setData({
            city3Data: res.data
          })
          for (var i = 0; i < res.data.length; i++) {
            city[2].push(res.data[i].name)
          }

          _this.setData({
            city: city
          })
        })
      })
    })
  },

  // 默认地址
  change: function(e) {
    var tag = 1;
    if (e.detail.value == '') {
      tag = 0;
    }
    this.setData({
      status: tag
    })
  },

  bindMultiPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },

  bindMultiPickerColumnChange: function(e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var _this = this;
    var city = this.data.city;
    var multiIndex = this.data.multiIndex;
    multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        api.ajax(service.url('getZoneList'), {
          
          pid: this.data.cityData[e.detail.value].zone_id
        }, function(res) {
          var city2Data = res.data;

          city[1] = [];
          for (var i = 0; i < res.data.length; i++) {
            city[1].push(res.data[i].name)
          }

          //--获取第三列的数据
          api.ajax(service.url('getZoneList'), {
            
            pid: city2Data[0].zone_id
          }, function(res) {
            var city3Data = res.data;

            city[2] = [];
            for (var i = 0; i < res.data.length; i++) {
              city[2].push(res.data[i].name)
            }

            multiIndex[1] = 0;
            multiIndex[2] = 0;

            _this.setData({
              city2Data: city2Data,
              city3Data: city3Data,
              multiIndex: multiIndex,
              city: city
            })
          })
        })
        break;
      case 1:
        api.ajax(service.url('getZoneList'), {
          
          pid: this.data.city2Data[e.detail.value].zone_id
        }, function(res) {
          var city3Data = res.data;

          city[2] = [];
          for (var i = 0; i < res.data.length; i++) {
            city[2].push(res.data[i].name)
          }
          multiIndex[2] = 0;

          _this.setData({
            city3Data: city3Data,
            multiIndex: multiIndex,
            city: city
          })
        })
        break;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {


  }
})